package com.neusoft.mapper;

import org.apache.ibatis.annotations.*;

import com.neusoft.po.DailyTotal;
import com.neusoft.po.Orders;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface OrdersMapper {

    @Insert("insert into orders(userId,businessId,orderDate,orderTotal,daId,orderState,addMark,payDate) values(#{userId},#{businessId},#{orderDate},#{orderTotal},#{daId},0,null,null)")
    @Options(useGeneratedKeys=true,keyProperty="orderId",keyColumn="orderId")
    public int saveOrders(Orders orders);

    public Orders getOrdersById(Integer orderId);

    public List<Orders> listOrdersByUserId(String userId);

    @Update("update orders set orderState=1, payDate=#{currentDate} where orderId=#{orderId}")
    int turnOrderState(@Param("orderId") Integer orderId, @Param("currentDate") String currentDate);

    public List<Orders> listPaidOrdersByUserId(String userId);

    @Update("update orders set addMark=#{addMark} where orderId=#{orderId}")
    public int updateAddMark(@Param("orderId") Integer orderId, @Param("addMark") Integer addMark);

    @Select("select date(payDate) as date, sum(orderTotal)as amount\n" +
            "\t\tfrom orders where userId = #{userId}\n" +
            "\t\t  and STR_TO_DATE(payDate, '%Y-%m-%d') between DATE_SUB(STR_TO_DATE(#{currentDate}, '%Y-%m-%d'), INTERVAL 1 WEEK) and STR_TO_DATE(#{currentDate}, '%Y-%m-%d')\n" +
            "\t\tgroup by date(payDate) order by date(payDate) asc;")
    public List<DailyTotal> listWeeklyTotalById(@Param("userId") String userId, @Param("currentDate") String currentDate);

    @Select("select date(payDate) as date, sum(orderTotal)as amount\n" +
            "\t\tfrom orders where userId = #{userId}\n" +
            "\t\t  and STR_TO_DATE(payDate, '%Y-%m-%d') between DATE_SUB(STR_TO_DATE(#{currentDate}, '%Y-%m-%d'), INTERVAL 1 MONTH) and STR_TO_DATE(#{currentDate}, '%Y-%m-%d')\n" +
            "\t\tgroup by date(payDate) order by date(payDate) asc;")
    public List<DailyTotal> listMonthlyTotalById(@Param("userId") String userId, @Param("currentDate") String currentDate);

    @Select("select sum(addMark) from orders where businessId = #{businessId} and orderState=1")
    public int getAllMarkById(Integer businessId);

    @Select("select count(*) from orders where businessId = #{businessId} and orderState=1")
    public int getNumberById(Integer businessId);

}

